<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class retail extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('login')) {
            redirect('login');
        } else {
            $this->load->model('mretail');
            $this->load->model('mtransaksi_jual');
            $this->load->model('mstok_limit');
            $this->load->model('mhutang');
        }
    }

    function index() {
        $data['title'] = '';
        $data['page'] = 'vall_retail';
        $data['data'] = $this->mretail->get_all()->result();
        $this->load->view('index', $data);
    }

    function view() {
        $koderetail = $this->uri->segment(3);
        $namaretail = $this->mretail->get_by_id('koderetail', $koderetail)->row()->namaretail;
        $data['title'] = 'Retail - ' . $namaretail;
        $data['page'] = 'vretail';

        $this->load->view('index', $data);
    }

    function vtransaksi() {
        $koderetail = $this->input->post('koderetail');

        $data['title'] = '';
        $data['stok_limit'] = $this->mstok_limit->get_stok_limit_by_retail($koderetail)->result();

        $retail = $this->mretail->get_by_id('koderetail', $koderetail)->row();
        $transaksi = $this->mtransaksi_jual->get_chart_per_hari_by_retail(date('m'), date('Y'), $koderetail);

        $bln = "";
        $total = "";
        foreach ($transaksi->result() as $row) {
            $bln = $bln . "','" . $row->tglbln;
            $total = $total . ',' . $row->total;
        }
        $bln = substr($bln, 2) . "'";
        $total = substr($total, 1);
        $data['name'] = $retail->namaretail;
        $data['bulan'] = $bln;
        $data['tahun'] = date('Y');
        $data['total'] = $total;

        $trxbarang = $this->mtransaksi_jual->get_chart_barang_per_bulan_by_retail($koderetail);

        $bulan_barang = "";
        $nama_barang = array();
        $total_barang = array();
        $array_data = array();

        $i = 0;
        foreach ($trxbarang->result() as $row) {
            $bulan_barang = $row->bln;
            $nama_barang[$i] = $row->namabarang;
            $array_data[$i] = $row->total;

            $arr['name'] = $row->namabarang;
            $arr['data'] = array(doubleval($row->total));

            $array_data[$i] = $arr;
            $i++;
        }

        $data['array_barang'] = json_encode($array_data);
        $data['bulan_barang'] = $bulan_barang;
        $this->load->view('retail/vtransaksi', $data);
    }

    function vhutang() {
        $koderetail = $this->input->post('koderetail');
        $retail = $this->mretail->get_by_id('koderetail', $koderetail)->row();

        $data['title'] = 'Hutang';
        $data['grid'] = $this->mhutang->get_hutang_retail($koderetail)->result();
        $chart = $this->mhutang->get_chart_hutang_retail($koderetail);

        $supplier = "";
        $limit = "";
        foreach ($chart->result() as $row) {
            $supplier = $supplier . "','" . $row->idtrx;
            $limit = $limit . ',' . $row->selisih;
        }
        $supplier = substr($supplier, 2) . "'";
        $limit = substr($limit, 1);
        $data['name'] = $retail->namaretail;
        $data['supplier'] = $supplier;
        $data['limit'] = $limit;

        $this->load->view('retail/vhutang', $data);
    }

    function vpiutang() {
        $koderetail = $this->input->post('koderetail');
        $retail = $this->mretail->get_by_id('koderetail', $koderetail)->row();

        $data['title'] = 'Piutang';
        $data['grid'] = $this->mhutang->get_piutang_retail($koderetail)->result();
        $chart = $this->mhutang->get_chart_piutang_retail($koderetail);

        $customer = "";
        $limit = "";
        foreach ($chart->result() as $row) {
            $customer = $customer . "','" . $row->idtrx;
            $limit = $limit . ',' . $row->selisih;
        }
        $customer = substr($customer, 2) . "'";
        $limit = substr($limit, 1);
        $data['name'] = $retail->namaretail;
        $data['customer'] = $customer;
        $data['limit'] = $limit;

        $this->load->view('retail/vpiutang', $data);
    }

}

/* End of file retail.php */
/* Location: ./application/controllers/retail.php */